package com.colorlight.cloud.xm.entity.po;

import com.colorlight.cloud.gateway.api.message.constraint.basics.object.Trans;
import com.colorlight.iot.proto.xm.entity.XmEntity;
import lombok.*;
import lombok.extern.slf4j.Slf4j;

import java.time.DateTimeException;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author Demon
 * @description 设备加解密信息
 * @date 28/4/2024  下午2:41
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class Time implements Trans<Time, XmEntity.Time> {
	
	/**
	 * 年 2024
	 * 月 01
	 * 日 22
	 * 时 18
	 * 分 20
	 * 秒 22
	 * // ，没有时区的概念
	 */
	private LocalDateTime dateTime;
	
	
	@Override
	public XmEntity.Time toProto() {
		LocalDateTime dateTime = this.getDateTime();
		XmEntity.Time.Builder builder = XmEntity.Time.newBuilder();
		builder.setYear(dateTime.getYear());
		builder.setMonth(dateTime.getMonthValue());
		builder.setDay(dateTime.getDayOfMonth());
		builder.setHour(dateTime.getHour());
		builder.setMinute(dateTime.getMinute());
		builder.setSecond(dateTime.getSecond());
		return builder.build();
	}
	
	@Override
	public Time formProto(XmEntity.Time time) {
		if (Objects.isNull(time) || time.getYear() == 0 || time.getMonth() == 0 || time.getDay() == 0) {
			return null;
		}
		LocalDateTime dateTime;
		try {
			dateTime = LocalDateTime.of(
					time.getYear(),
					time.getMonth(),
					time.getDay(),
					time.getHour(),
					time.getMinute(),
					time.getSecond());
			this.setDateTime(dateTime);
		} catch (DateTimeException e) {
			log.warn("时间异常" + time);
		}
		return this;
	}
}
